﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Maticsoft.DAL;
using System.Reflection;
using System.IO;
using Commons.CommonHelper;
using BuildingForm.Common;
using BuildingForm.PersonInfo;
using System.Configuration;


namespace BuildingForm
{
    public partial class SplitScreenStatisticsForm5 : Form
    {
           private int RyID = 0;
        System.Timers.Timer myTimer; 
        public SplitScreenStatisticsForm5()
        {
            InitializeComponent();
            Init();
            bind();
        }

        private void Init()
        {
            this.lblName.Text = "";
            this.label12.Text = "";
            this.label13.Text = "";
            myTimer = new System.Timers.Timer(1000);//定时周期2秒
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);//到2秒了做的事件
            myTimer.AutoReset = true; //是否不断重复定时器操作
            myTimer.Start();
        }

        void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            bind();
        }

        private void bind()
        {
            Maticsoft.DAL.Kqjl_Info_Copy kqDao = new Kqjl_Info_Copy();
            Maticsoft.DAL.Jsd_Info jsdDao = new Jsd_Info();
            string jsdm = string.Empty;
            if (System.Configuration.ConfigurationManager.AppSettings["ScreenForm5"] != null)
            {
                jsdm = System.Configuration.ConfigurationManager.AppSettings["ScreenForm5"].ToString();
            }
            string sql = " 1=1";
            if (!string.IsNullOrEmpty(jsdm) && jsdm != "全部")
            {
                sql += " and a.jsdm='" + jsdm + "'";
            }
            else
            {
                jsdm = "";
            }
            sql += " order by a.kqsj desc";
            DataTable dt2 = kqDao.GetListByTopOne(sql).Tables[0];
            if (dt2.Rows.Count == 0)
            {
                DataTable dt = jsdDao.GetList(" jsdm='" + jsdm + "'").Tables[0];
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("配置文件设置错误！"); return;
                }
                //MessageBox.Show("数据为空！");
                return;
            }
            DataRow dr = dt2.Rows[0];
            int id = Convert.ToInt32(dr["kqlsh"]);
            if (RyID == 0)
            {
                RyID = id;
            }
            else
            {
                if (RyID == id) return;
            }
            string xm = dr["xm"].ToString();
            string gz = dr["gz"].ToString();
            string state = Convert.ToInt32(dr["jczt"]) == 1 ? "进" : "出";
            string time = Convert.ToDateTime(dr["kqsj"]).ToString("yyyy-MM-dd HH:mm:ss");
            string company = dr["fbmc"] == null ? "" : dr["fbmc"].ToString();
            string bz = dr["qybmm"].ToString();
            string channel = dr["jsdm"].ToString();
            string sfzh = dr["sfzh"].ToString();
            string str = System.Windows.Forms.Application.StartupPath;
            //string str2 = str.Substring(0, str.LastIndexOf("\\"));
            string str2 = str;
            string path1 = str2 + @"\Photos\" + sfzh + ".jpg";
            string zpxp = dr["zpxp"] == null ? "" : dr["zpxp"].ToString();
            string path2 = str2 + @"\CatchPhotos\" + zpxp;
            //赋值
            this.lblchannel.Text = channel;
            this.lblcompany.Text = company;
            this.lblGZ.Text = gz;
            this.lblName.Text = xm;
            this.lblState.Text = state;
            this.lblTeam.Text = bz;
            this.lblTime.Text = time;
            if (File.Exists(path1))
            {
                this.pictureBox1.BackgroundImage = Image.FromFile(path1);
            }
            else
            {
                this.pictureBox1.BackgroundImage = null;
            }
            if (File.Exists(path2))
            {
                this.pictureBox2.BackgroundImage = Image.FromFile(path2);
            }
            else
            {
                this.pictureBox2.BackgroundImage = null;
            }
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.BackgroundImageLayout = ImageLayout.Stretch;
            //统计
            DataTable dt3 = kqDao.GetListBySplitScreenStatistics(jsdm).Tables[0];
            if (dt3.Rows.Count == 1)
            {
                string num = dt3.Rows[0]["CountNum"].ToString();
                this.label12.Text = num;
                this.listBox1.Items.Add("无");
            }
            else
            {
                string num = dt3.Rows[0]["CountNum"].ToString();
                this.label12.Text = num;
                StringBuilder strBuilder = new StringBuilder();
                for (var i = 1; i < dt3.Rows.Count; i++)
                {
                    string nbz = dt3.Rows[i]["bz"].ToString();
                    string count = dt3.Rows[i]["CountNum"].ToString();
                    strBuilder.Append(nbz+"："+count+"\n");
                }
                this.listBox1.Items.Add(strBuilder.ToString());
            }
        }

        ///// <summary>
        ///// esc事件
        ///// </summary>
        ///// <param name="msg"></param>
        ///// <param name="keyData"></param>
        ///// <returns></returns>
        //protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        //{
        //    int WM_KEYDOWN = 256;
        //    int WM_SYSKEYDOWN = 260;
        //    if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
        //    {
        //        switch (keyData)
        //        {
        //            case Keys.Escape:
        //                this.FormBorderStyle = FormBorderStyle.Fixed3D;
        //                this.WindowState = FormWindowState.Normal;
        //                break;
        //        }
        //    }
        //    return false;
        //}

        //protected override void WndProc(ref Message m)
        //{
        //    const int WM_NCLBUTTONDBLCLK = 0xA3;
        //    switch (m.Msg)
        //    {
             
        //        case WM_NCLBUTTONDBLCLK:// 双击最大化
        //            esc();
        //            return;
        //        default:
        //            base.WndProc(ref m);
        //            break;
        //    }
        //}

        /// <summary>
        /// 退出全屏
        /// </summary>
        private void esc()
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.FormBorderStyle = FormBorderStyle.Fixed3D;
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }
        }

        private void SplitScreenStatisticsForm_DoubleClick(object sender, EventArgs e)
        {
            //esc();
        }
    }
}
